
import pymysql
from common.log import logger
from common.read import FileRead

data = FileRead().read_ini()["mysql"]
DB_CONF = {
    "host": data['MYSQL_HOST'],
    "port": int(data['MYSQL_PORT']),
    "user": data['MYSQL_USER'],
    "password": data['MYSQL_PASSWD'],
    "db": data['MYSQL_DB']
}

class MysqlDb:
    def __init__(self):
        #连接数据库
        self.conn = pymysql.connect(**DB_CONF,autocommit=True)
        #创建一个游标对象
        self.cur = self.conn.cursor()

    #关闭连接
    def __del__(self):
        self.cur.close()
        self.conn.close()

    #查询数据
    def select_db(self,sql):
        logger.info(f'执行sql：{sql}')
        try:
            self.cur.execute(sql)
        except Exception as e:
            logger.info(f'执行{sql}出错:{e}')
        #获取数据
        result = self.cur.fetchone()
        logger.info(f'result值为:{result[0]}')
        return result[0]

    #删除数据
    def delete_info(self,sql):
        logger.info(f'执行sql：{sql}')
        try:
            self.cur.execute(sql)
        except Exception as e:
            logger.info(f'执行{sql}出错:{e}')

db = MysqlDb()




